package com.example.tianling.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.tianling.admin.service.CarouselService;
import com.example.tianling.admin.service.GoodService;
import com.example.tianling.admin.vo.CarouselVo;
import com.example.tianling.common.annotation.Login;
import com.example.tianling.common.annotation.LoginUser;
import com.example.tianling.common.utils.R;
import com.example.tianling.entity.CarouselEntity;
import com.example.tianling.entity.ProductEntity;
import com.example.tianling.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Controller
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    @Login
    @PostMapping("/carousel/uploadCarousel.do")
    @ResponseBody
    public R uploadCarousel(@RequestBody CarouselEntity carouselEntity){
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化时间
        String formattedDateTime = now.format(formatter);
        carouselEntity.setCreatedTime(formattedDateTime);
        return R.success(carouselService.uploadCarousel(carouselEntity));
    }
//更新数据
    @Login
    @PostMapping("/carousel/updataCarousel.do")
    @ResponseBody
    public R updataCarousel(@RequestBody CarouselEntity carouselEntity){
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化时间
        String formattedDateTime = now.format(formatter);
        carouselEntity.setUpdatedTime(formattedDateTime);
        return R.success(carouselService.uodataCarousel(carouselEntity));
    }
    //返回一个离职
    @Login
    @PostMapping("/carousel/oneCarousel.do")
    @ResponseBody
    public R oneCarousel(@RequestBody String params){
        // 获取当前时间
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String id = paramsJSONObject.getString("id");
        return R.success(carouselService.getOneCarousel(id));
    }
    //返回轮播图清单
    @Login
    @PostMapping("/carousel/allCarousel.do")
    @ResponseBody
    public R allCarousel(){
        // 获取当前时间
        return R.success(carouselService.getAllCarousel());
    }

    //返回轮播图清单
    @Login
    @PostMapping("/carousel/deleteCarousel.do")
    @ResponseBody
    public R deleteCarousel(@RequestBody String params){
        // 获取当前时间
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String id = paramsJSONObject.getString("id");
        return R.success(carouselService.deleteCarousel(id));
    }
}
